package com.accloud.cloudservice;

import android.app.Application;
import android.content.Context;
import android.os.AsyncTask;
import com.accloud.common.ACConfiguration;
import com.accloud.service.ACAccountMgr;
import com.accloud.service.ACBindMgr;
import com.accloud.service.ACClassDataMgr;
import com.accloud.service.ACContext;
import com.accloud.service.ACDataMgr;
import com.accloud.service.ACDeviceDataMgr;
import com.accloud.service.ACDeviceFind;
import com.accloud.service.ACDeviceMgr;
import com.accloud.service.ACDeviceMsg;
import com.accloud.service.ACDeviceTimerMgr;
import com.accloud.service.ACException;
import com.accloud.service.ACFeedbackMgr;
import com.accloud.service.ACFileMgr;
import com.accloud.service.ACMsg;
import com.accloud.service.ACOTAMgr;
import com.accloud.service.ACProductMgr;
import com.accloud.service.ACPushMgr;
import com.accloud.service.ACRankingMgr;
import com.accloud.service.ACTimerMgr;
import com.accloud.service.ACUserTimerMgr;
import com.accloud.service.ACWeatherMgr;
import com.accloud.utils.ACUtils;
import com.accloud.utils.PreferencesUtils;
import com.fasterxml.jackson.core.util.BufferRecycler;
import com.hiflying.smartlink.ISmartLinker;
import cz.msebera.android.httpclient.HttpStatus;
import cz.msebera.android.httpclient.protocol.HttpRequestExecutor;
import java.util.List;
import java.util.TimeZone;
import org.json.JSONException;

public class AC {
    public static final int CLOUD_FIRST = 4;
    public static int DEVICE_ACTIVATOR_DEFAULT_TIMEOUT = ISmartLinker.DEFAULT_TIMEOUT_PERIOD;
    public static final int DEVICE_AP = -2;
    public static int FIND_DEVICE_DEFAULT_TIMEOUT = HttpRequestExecutor.DEFAULT_WAIT_FOR_CONTINUE;
    public static int INIT_APP_DEFAULT_TIMEOUT = BufferRecycler.DEFAULT_WRITE_CONCAT_BUFFER_LEN;
    public static final int LOCAL_FIRST = 3;
    public static final int ONLY_CLOUD = 2;
    public static final int ONLY_LOCAL = 1;
    public static final int PRODUCTION_MODE = 1;
    public static final int REGIONAL_CENTRAL_EUROPE = 4;
    public static final int REGIONAL_CHINA = 0;
    public static final int REGIONAL_EAST_CHINA = 2;
    public static final int REGIONAL_NORTH_AMERICA = 3;
    public static final int REGIONAL_SOUTHEAST_ASIA = 1;
    public static int SEND_TO_LOCAL_DEVICE_DEFAULT_TIMEOUT = HttpRequestExecutor.DEFAULT_WAIT_FOR_CONTINUE;
    public static final int TEST_MODE = 0;
    private static ACAccountMgr accountMgr;
    private static String address;
    private static ACBindMgr bindMgr;
    private static ACClassDataMgr classDataMgr;
    public static Context context;
    private static ACDataMgr customDataMgr;
    private static ACDeviceDataManager deviceDataMgr;
    static ACDeviceLocalManager deviceLocalManager = new ACDeviceLocalManager();
    private static ACDeviceMgr deviceMgr;
    private static ACFeedbackMgr feedbackMgr;
    private static ACFileMgr fileMgr;
    public static String majorDomain;
    public static long majorDomainId;
    private static ACOTAMgr otaMgr;
    public static String phoneId;
    private static ACProductMgr productMgr;
    private static ACPushMgr pushMgr;
    private static ACRankingMgr rankingMgr;
    static ACEventManager sEventManager;
    static ACSocketManager sSockectManager = new ACSocketManager();
    private static ACWeatherMgr weatherMgr;

    public interface LocalDataReceiver {
        void onDataReceive(long j, String str, ACDeviceMsg aCDeviceMsg);
    }

    public interface LocalDeviceObserver {
        void onDeviceChanged(ACDeviceFind aCDeviceFind, boolean z);
    }

    public static void init(Application app, String MajorDomain, long MajorDomainId) {
        init(app, MajorDomain, MajorDomainId, 1);
    }

    public static void init(Application app, String MajorDomain, long MajorDomainId, int mode) {
        init(app, MajorDomain, MajorDomainId, mode, 0);
    }

    public static void init(Application app, String MajorDomain, long MajorDomainId, int mode, int regional) {
        context = app.getApplicationContext();
        majorDomain = MajorDomain;
        majorDomainId = MajorDomainId;
        ACConfiguration.setMode(mode);
        ACConfiguration.setRegional(regional);
        phoneId = ACUtils.getPhoneId(context);
        sEventManager = new ACEventManager(context);
    }

    public static void setSendToLocalUDS(String address2) {
        address = address2;
    }

    public static void setRouterAddress(String ROUTER_ADDR) {
        ACConfiguration.setRouterAddr(ROUTER_ADDR);
    }

    public static void setRegional(int regional) {
        ACConfiguration.setRegional(regional);
    }

    public static void sendToService(String name, int version, ACMsg req, PayloadCallback<ACMsg> callback) {
        sendToService(null, name, version, req, callback);
    }

    public static void sendToService(String subDomain, String name, int version, ACMsg req, PayloadCallback<ACMsg> callback) {
        String str;
        if (req != null) {
            str = req.getName();
        } else {
            str = null;
        }
        final PayloadEventCallback<ACMsg> callbackWrapper = new PayloadEventCallback<>(new ServiceEvent("anonymity", name, version, str), callback);
        if (name.length() <= 0 || version <= 0 || req == null) {
            callbackWrapper.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (PreferencesUtils.getLong(context, ACConfiguration.KEY_USERID, -1) <= 0) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            req.setContext(subDomain);
            new ACServiceClient(name, version, address == null ? ACConfiguration.getRouterAddr() : address, req, callbackWrapper, new ACMsgListener() {
                /* class com.accloud.cloudservice.AC.AnonymousClass1 */

                @Override // com.accloud.cloudservice.ACMsgListener
                public void finish(ACMsg resp) {
                    callbackWrapper.success(resp);
                }
            }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Void[0]);
        }
    }

    public static void sendToServiceWithoutSign(String name, int version, ACMsg req, PayloadCallback<ACMsg> callback) {
        sendToServiceWithoutSign(null, name, version, req, callback);
    }

    public static void sendToServiceWithoutSign(String subDomain, String name, int version, ACMsg req, PayloadCallback<ACMsg> callback) {
        String str;
        if (req != null) {
            str = req.getName();
        } else {
            str = null;
        }
        final PayloadEventCallback<ACMsg> callbackWrapper = new PayloadEventCallback<>(new ServiceEvent("normal", name, version, str), callback);
        if (name.length() <= 0 || version <= 0 || req == null) {
            callbackWrapper.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
            return;
        }
        ACContext context2 = new ACContext(majorDomain, subDomain, 0);
        context2.setAccessMode("1");
        req.setContext(context2);
        new ACServiceClient(name, version, address == null ? ACConfiguration.getRouterAddr() : address, req, callbackWrapper, new ACMsgListener() {
            /* class com.accloud.cloudservice.AC.AnonymousClass2 */

            @Override // com.accloud.cloudservice.ACMsgListener
            public void finish(ACMsg resp) {
                callbackWrapper.success(resp);
            }
        }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Void[0]);
    }

    public static void findLocalDevice(int timeout, PayloadCallback<List<ACDeviceFind>> callback) {
        deviceLocalManager.findDevice(timeout, HttpStatus.SC_INTERNAL_SERVER_ERROR, callback);
    }

    public static void stopFindLocalDevice() {
        deviceLocalManager.stopFind();
    }

    public static void findLinkingDevice(int timeout, PayloadCallback<ACDeviceFind> callback) {
        deviceLocalManager.findLinkingDevice(timeout, HttpStatus.SC_INTERNAL_SERVER_ERROR, callback);
    }

    public static void stopFindLinkingDevice() {
        deviceLocalManager.stopFind();
    }

    public static void registerLocalDataReceiver(LocalDataReceiver receiver) {
        deviceLocalManager.registerLocalDataReceiver(receiver);
    }

    public static void unregisterLocalDataReceiver(LocalDataReceiver receiver) {
        deviceLocalManager.unregisterLocalDataReceiver(receiver);
    }

    public static void registerLocalDeviceObserver(LocalDeviceObserver observer) {
        deviceLocalManager.registerLocalDeviceObserver(observer);
    }

    public static void unregisterLocalDeviceObserver(LocalDeviceObserver observer) {
        deviceLocalManager.unregisterLocalDeviceObserver(observer);
    }

    public static void getLocalDeviceWifiLinkQuality(List<ACDeviceFind> deviceFindList, int timeoutMs, PayloadCallback<List<ACDeviceFind>> callback) {
        deviceLocalManager.getLocalDeviceWifiLinkQuality(deviceFindList, timeoutMs, callback);
    }

    public static ACAccountMgr accountMgr() {
        if (accountMgr == null) {
            accountMgr = new ACAccountManager();
        }
        return accountMgr;
    }

    public static ACFeedbackMgr feedbackMgr() {
        if (feedbackMgr == null) {
            feedbackMgr = new ACFeedbackManager();
        }
        return feedbackMgr;
    }

    public static ACDeviceActivator deviceActivator(int deviceType) {
        return ACDeviceActivator.of(deviceType);
    }

    public static ACBindMgr bindMgr() {
        if (bindMgr == null) {
            bindMgr = new ACBindManager();
        }
        return bindMgr;
    }

    public static ACDeviceMgr deviceMgr() {
        if (deviceMgr == null) {
            deviceMgr = new ACDeviceManager();
        }
        return deviceMgr;
    }
    public static synchronized ACPushMgr pushMgr() {
        ACPushMgr aCPushMgr;
        synchronized (AC.class) {
            if (pushMgr == null) {
                pushMgr = new ACPushManager();
            }
            aCPushMgr = pushMgr;
        }
        return aCPushMgr;
    }

    public static ACClassDataMgr classDataMgr() {
        if (classDataMgr == null) {
            classDataMgr = new ACClassDataManager();
        }
        return classDataMgr;
    }

    public static ACDeviceDataMgr deviceDataMgr() {
        if (deviceDataMgr == null) {
            deviceDataMgr = new ACDeviceDataManager();
        }
        return deviceDataMgr;
    }

    public static ACDataMgr customDataMgr() {
        if (customDataMgr == null) {
            customDataMgr = new ACDataManager();
        }
        return customDataMgr;
    }

    @Deprecated
    public static ACTimerMgr timerMgr() {
        return new ACTimerManager();
    }

    @Deprecated
    public static ACTimerMgr timerMgr(TimeZone timeZone) {
        return new ACTimerManager(timeZone);
    }

    public static ACDeviceTimerMgr deviceTimerMgr(long deviceId) {
        return new ACDeviceTimerManager(deviceId);
    }

    public static ACDeviceTimerMgr deviceTimerMgr(long deviceId, TimeZone timeZone) {
        return new ACDeviceTimerManager(deviceId, timeZone);
    }

    public static ACUserTimerMgr userTimerMgr() {
        return new ACUserTimerManager();
    }

    public static ACUserTimerMgr userTimerMgr(TimeZone timeZone) {
        return new ACUserTimerManager(timeZone);
    }

    public static ACOTAMgr otaMgr() {
        if (otaMgr == null) {
            otaMgr = new ACOTAManager();
        }
        return otaMgr;
    }

    public static synchronized ACFileMgr fileMgr() {
        ACFileMgr aCFileMgr;
        synchronized (AC.class) {
            if (fileMgr == null) {
                fileMgr = new ACFileManager();
            }
            aCFileMgr = fileMgr;
        }
        return aCFileMgr;
    }

    public static ACWeatherMgr weatherMgr() {
        if (weatherMgr == null) {
            weatherMgr = new ACWeatherManager();
        }
        return weatherMgr;
    }

    public static ACRankingMgr rankingMgr() {
        if (rankingMgr == null) {
            rankingMgr = new ACRankingManager();
        }
        return rankingMgr;
    }

    public static ACProductMgr productMgr() {
        if (productMgr == null) {
            productMgr = new ACProductManager();
        }
        return productMgr;
    }

    /* access modifiers changed from: private */
    public static class ServiceEvent extends ACEvent {
        private ServiceEvent(String eventName, String serviceName, int serviceVersion, String method) {
            super("apm_app_uds_service", eventName, null);
            try {
                this.data.put("service_name", serviceName);
                this.data.put("service_version", serviceVersion);
                this.data.put("method", method);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
